﻿//
// Created by work on 24-6-6.
//

#include "CRtModelYkInfo.h"

std::map<CRtModelYkInfo::EModelYkCol, QString> CRtModelYkInfo::rtModelYkColToStringMap = {
    {CRtModelYkInfo::EEn_write, "en_write"},
    {CRtModelYkInfo::EValues, "valuesmap"}
};

std::map<CRtModelYkInfo::EModelYkCol, QString> CRtModelYkInfo::ykColFieldsNameMap = {
    {CRtModelYkInfo::EModelYkCol::EEn_write, "是否允许写入"},
    {CRtModelYkInfo::EModelYkCol::EValues, "值映射"}
};

std::map<CRtModelYkInfo::EModelYkCol, CValue::EValueType> CRtModelYkInfo::rtYkDataTypeByField = {
    {CRtModelYkInfo::EEn_write, CValue::IOT_Bool},
    {CRtModelYkInfo::EValues, CValue::IOT_String}
};

CRtModelYkInfo::CRtModelYkInfo(QObject *parent) : CRtModelSigBase(YK_COL_SIZE, parent) {
    QMap<int, QString> vmap;
    vmap.insert(0, "否");
    vmap.insert(1, "是");
    m_fieldValueMap.insert(ykColFieldName(YK_ENUM::EEn_write), vmap);

    m_enPluginEdit.insert(ykColFieldName(YK_ENUM::EValues), true);
    m_fieldPluginMap.insert(ykColFieldName(YK_ENUM::EValues), "yxvalueeditplugin");
}

QJsonArray CRtModelYkInfo::valuesToJson() const
{
    QJsonArray arr;
    for(const auto& it : values) {
        arr.append(it.toJson());
    }
    return arr;
}

void CRtModelYkInfo::valuesFromJson(QJsonArray json)
{
    values.clear();
    if(json.isEmpty())
        return;

    for(const auto& obj : json) {
        SYkValue value;
        value.fromJson(obj.toObject());
        values.push_back(value);
    }
}

QString CRtModelYkInfo::getFieldNameByCol(int col) {
    if(col < 0 || col > (YK_COL_SIZE-1))
        return "";

    if(col < SIGBASE_COL_SIZE)
        return CRtModelSigBase::getFieldNameByCol(col);

    return ykColFieldName(static_cast<EModelYkCol>(col));
}

QVariant CRtModelYkInfo::getFieldValueByFielddName(const QString &name) {
    if(name.isEmpty())
        return QVariant();

    if(ykColFieldName(YK_ENUM::EValues) == name) {
        QJsonObject obj;
        obj[ykFieldName(YK_ENUM::EValues)] = valuesToJson();
        return QString(QJsonDocument(obj).toJson(QJsonDocument::Compact));
    } else if(ykColFieldName(YK_ENUM::EEn_write) == name){
        return en_write ? 1 : 0;
    } else {
        return CRtModelSigBase::getFieldValueByFielddName(name);
    }
}

void CRtModelYkInfo::setFieldValueByCol(int col, const QVariant &value)
{

}

QJsonObject CRtModelYkInfo::toJson() {
    QJsonObject json = CRtModelSigBase::toJson();
    json[ykFieldName(YK_ENUM::EEn_write)] = en_write;
    json[ykFieldName(YK_ENUM::EValues)] = valuesToJson();
    return json;
}

void CRtModelYkInfo::fromJson(const QJsonObject &json) {
    CRtModelSigBase::fromJson(json);
    en_write = json[ykFieldName(YK_ENUM::EEn_write)].toBool();
    valuesFromJson(json[ykFieldName(YK_ENUM::EValues)].toArray());
}

QSharedPointer<IModel> CRtModelYkInfo::createEmpty()
{

}

bool CRtModelYkInfo::sync(bool dir)
{

}
